﻿using System;
using System.Web.Mvc;
using MvcContrib;
using MvcContrib.ActionResults;
using MvcContrib.Filters;
using ScrumBox.Model;
using ScrumBox.Web.Helpers;

namespace ScrumBox.Web.Controllers
{
    public class ProjectController : Controller
    {
        private readonly IProjectRepository _projectRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectRepository"></param>
        public ProjectController(IProjectRepository projectRepository)
        {
            _projectRepository = projectRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult List()
        {
            var projects = _projectRepository.GetAllProjects();
            ViewData.Add(projects);
            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public XmlResult XmlList()
        {
            return new XmlResult(_projectRepository.GetAllProjects());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public ActionResult Details(string key)
        {
            var project = _projectRepository.GetByKey(key);
            ViewData.Add(project);
            return View();
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public ActionResult Edit(string key)
        {
            ViewData.Add(_projectRepository.GetByKey(key));
            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult New()
        {
            ViewData.Add(new Project());
            return View("edit");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectName"></param>
        /// <param name="projectKey"></param>
        /// <param name="projectStart"></param>
        /// <param name="projectDesc"></param>
        /// <returns></returns>
        [PostOnly]
        public ActionResult Save(string projectName, string projectKey, DateTime projectStart, string projectDesc)
        {
            var project = _projectRepository.GetByKey(projectKey) ?? new Project();

            project.Description = projectDesc;
            project.Key = projectKey;
            project.Name = projectName;
            project.StartDate = projectStart;

            try
            {
                _projectRepository.Save(project);
                TempData[TempDataKeys.Message] = "The conference was created successfully.";
                return RedirectToRoute("ProjectList");
            }
            catch (Exception e)
            {
                //Log.Error("Error saving project.", e);
                TempData[TempDataKeys.Error] = "There was an error saving the project.  The error was: " + e;
                ViewData.Add("project", project);
                return View("edit");
            }
        }
    }
}